<?php

/**
 * Created by Sven.
 * Date: 2016/12/25
 * Time: 12:31
 * Nav 控制器
 */
class NavAction extends Action {

    public function __construct( $_tpl ) {
        parent::__construct( $_tpl, new NavModel( $_tpl ) );
        // 3 在进行一系列action之后，显示界面
//        $this->tpl->display( 'main_home_nav.tpl' );
    }


    public function action() {
        if (!isset($_GET['action'])){
            $_GET['action'] = 'show';
        }

        // 使用?action=show 区分同一个页面的四个模块
        switch ( $_GET[ 'action' ] ) {
            case 'add':
                $this->add();
                break;
            case 'delete':
                $this->delete();
                break;
            case 'update':
                $this->update();
                break;
            case 'show':
                $this->show();
                break;
            case 'addchild':
                $this->addchild();
                break;
            case 'showchild':
                $this->showchild();
                break;
            case 'sort':
                $this->sort();
                break;
            default:
                exit( 'ERROR：action参数非法' );
                break;
        }
    }

    // 新增导航
    private function add() {
        // 在显示页面元素前，处理一下接收的参数
        if (  IS_POST ) {

            if ( Validate::checkNull( $_POST[ 'nav_name' ] ) ) Tool::alertBack( '警告：导航名称不能为空' );
            if ( Validate::checkMinLengh( $_POST[ 'nav_name' ], 2 ) ) Tool::alertBack( '警告：导航名称不能小于2位或大于20位' );
            if ( Validate::checkMaxLengh( $_POST[ 'nav_name' ], 20 ) ) Tool::alertBack( '警告：导航名称不能小于2位或大于20位' );
            if ( Validate::checkMaxLengh( $_POST[ 'nav_info' ], 200 ) ) Tool::alertBack( '警告：导航描述不能大于200位' );

            $this->model->nav_name = $_POST[ 'nav_name' ];
            $this->model->nav_info = $_POST[ 'nav_info' ];
            $this->model->sort = intval($_POST['sort']);

            $_nav_count = $this->model->getNavCountByName();
            if ($_nav_count > 0){
                Tool::alertBack('已经存在相同的导航名称');
            } else {
                $_result = $this->model->addNav();
                // 判断是否执行成功
                if ( $_result ) {
                    // 成功
                    // 返回之前的页面
                    $back_url = isset($_POST['prev_url']) ? $_POST['prev_url'] : PREV_URL;
                    Tool::alertLocation( '修改导航成功', $back_url );
                } else {
                    // 失败
                    Tool::alertBack( '新增导航失败' );
                }
            }


        }


        $this->tpl->assign( 'add', true );
        $this->tpl->assign( 'title', '新增导航' );
    }

    // 删除导航
    private function delete() {
        if ( isset( $_GET[ 'id' ] ) ) {
            $this->model->id = $_GET[ 'id' ];

            $_result = $this->model->deleteNav(); // true or false
            if ( $_result ) {
                // 返回之前的页面
                $back_url = isset($_POST['prev_url']) ? $_POST['prev_url'] : PREV_URL;
                Tool::alertLocation( '修改导航成功', $back_url );
            } else {
                Tool::alertBack( '删除导航失败' );
            }

        } else {
            Tool::alertBack( '非法操作：ID错误' );
        }
    }

    // 修改导航
    private function update() {
        // 在显示界面之前，判断是否提交了数据
        if (  IS_POST ) {
            $_POST['id'] = intval($_POST['id']);
            if ( isset( $_POST[ 'id' ] ) && $_POST['id'] > 0  ) {
                // 判断这个id是否对应真实的数据记录
                $_count = $this->model->getCount('cms_manage', 'id', $_POST['id']);
                if ($_count <= 0){
                    Tool::alertBack('警告：非法的ID值');
                    return;
                }


                if ( Validate::checkNull( $_POST[ 'nav_name' ] ) ) Tool::alertBack( '警告：导航名称不能为空' );
                if ( Validate::checkMinLengh( $_POST[ 'nav_name' ], 2 ) ) Tool::alertBack( '警告：导航名称不能小于2位或大于20位' );
                if ( Validate::checkMaxLengh( $_POST[ 'nav_name' ], 20 ) ) Tool::alertBack( '警告：导航名称不能小于2位或大于20位' );
                if ( Validate::checkMaxLengh( $_POST[ 'nav_info' ], 200 ) ) Tool::alertBack( '警告：导航描述不能大于200位' );

                $this->model->id = $_POST[ 'id' ];
                $this->model->nav_name = $_POST[ 'nav_name' ];
                $this->model->nav_info = $_POST[ 'nav_info' ];

                $_result = $this->model->updateNav();
                // 判断是否执行成功
                if ( $_result ) {
                    // 成功
                    // 返回之前的页面
                    $back_url = isset($_POST['prev_url']) ? $_POST['prev_url'] : PREV_URL;
                    Tool::alertLocation( '修改导航成功', $back_url );
                } else {
                    // 失败
                    Tool::alertBack( '修改导航失败' );
                }

            } else {
                Tool::alertBack( '非法操作：ID错误' );
            }

        }

        if ( $_GET[ 'id' ] ) {
            $this->model->id = $_GET[ 'id' ];
            $_row = $this->model->getSingleNav();
            if ( isset( $_row ) && is_object( $_row ) ) {
                $this->tpl->assign( 'data', $_row );
            } else {
                Tool::alertBack( '非法操作：ID错误' );
            }

        } else {
            Tool::alertBack( '非法操作：ID错误' );
        }

        $this->tpl->assign( 'update', true );
        $this->tpl->assign( 'title', '修改导航' );
    }

    // 显示导航
    private function show() {

        // 分页
        $_total =  $this->model->getCount('cms_nav', 'pid', '0');
        parent::page($_total, PAGE_SIZE);

        $_data = $this->model->getAllNavs();

        $this->tpl->assign( 'data', $_data );
        $this->tpl->assign( 'show', true );
        $this->tpl->assign( 'title', '导航列表' );

    }

    public function addchild(){
        if (IS_POST){
            $_pid = intval($_POST['pid']);
            if ($_pid > 0){
                $this->model->id = $_pid;
                $_nav_count = $this->model->getNavCountById();
                if ($_nav_count <= 0){
                    Tool::alertBack('不存在该导航');
                }

                $this->model->pid = $_pid;
                $this->model->nav_name = $_POST['nav_name'];
                $this->model->nav_info = $_POST['nav_info'];
                $_result = $this->model->addChildNav();
                if ($_result > 0){
                    Tool::alertLocation('添加成功',  $_POST['prev_url']);
                } else {
                    Tool::alertBack('添加失败');
                }

            } else {
                Tool::alertBack('非法的PID值');
            }
            return;
        }

        if (isset($_GET['id'])){
            $_id = intval($_GET['id']);
            if ($_id > 0){
                $this->model->pid = $_id;

                $this->tpl->assign( 'addchild', true );
                $this->tpl->assign( 'pid', $_GET['id'] );
                $this->tpl->assign( 'parent_nav_name', $_GET['nav_name'] );
                $this->tpl->assign( 'PREV_URL', PREV_URL );
                $this->tpl->assign( 'title', '添加子导航' );
            } else {
                Tool::alertBack('非法的 ID 值');
            }

        } else {
            Tool::alertBack('非法的 ID 值');

        }
    }

    private function showchild(){
        if (isset($_GET['id'])){
            $_id = intval($_GET['id']);
            if ($_id > 0){
                $this->model->pid = $_id;
                // 分页
                $_total =  $this->model->getChildTotalCount();
                parent::page($_total, PAGE_SIZE);

                $_data = $this->model->getAllChildNavs();

                // 获取父导航名称
                $this->model->id = $_GET['id'];
                $_parent_nav = $this->model->getSingleNav();

                $this->tpl->assign( 'data', $_data );
                $this->tpl->assign( 'nav_name', $_parent_nav ? $_parent_nav->nav_name : ''  );
                $this->tpl->assign( 'showchild', true );
                $this->tpl->assign( 'title', '子导航列表' );
            } else {
                Tool::alertBack('非法的 ID 值');
            }

        } else {
            Tool::alertBack('非法的 ID 值');

        }

    }

    private function sort(){
        if (IS_POST){
            $this->model->sort =  $_POST['sort'];
            $this->setSort();

            header('Location: '.PREV_URL);

        } else {
            exit('ERROR: 非法的请求');
        }
    }

    private function setSort() {
        return $this->model->setNavSort();
    }

    // 提供前台导航

    public function  showNav(){
        $_front_navs = $this->model->getFrontNav();
        $this->tpl->assign('frontNav', $_front_navs);
    }

    public function getNav(){
//        exit( isset($_GET['nav_id']) );

        if (isset($_GET['nav_id'])){
//            echo 'fsf'.$_GET['nav_id'];
//            exit;

            $_id = $_GET['nav_id'];
            $this->model->id = $_id;
            $this->model->pid = $_id;
            $_nav =  $this->model->getSingleNav();
            $_subNavs = $this->model->getAllNavs();
            if (is_object($_nav)){

                $this->tpl->assign('curr_id', $_nav->id);
                $this->tpl->assign('curr_nav', $_nav->nav_name);
                $this->tpl->assign('parent_id', $_nav->n2id);
                $this->tpl->assign('parent_nav', $_nav->n2nav_name);
            }
            if (isset($_subNavs)){
                $this->tpl->assign('subNavs', $_subNavs);
            }

        } else {
            exit('警告：非法的ID值');
        }

    }

    public function getNavById($_nav_id = 0){
        if (!empty($_nav_id)){

            $this->model->id = $_nav_id;
            $this->model->pid = $_nav_id;
            $_nav =  $this->model->getSingleNav();
            $_subNavs = $this->model->getAllNavs();
            if (is_object($_nav)){

                $this->tpl->assign('curr_id', $_nav->id);
                $this->tpl->assign('curr_nav', $_nav->nav_name);
                $this->tpl->assign('parent_id', $_nav->n2id);
                $this->tpl->assign('parent_nav', $_nav->n2nav_name);
            }
            if (isset($_subNavs)){
                $this->tpl->assign('subNavs', $_subNavs);
            }

        } else {
            exit('警告：非法的ID值');
        }


    }




}